#include "queue.h"

Queue *createQueue(){
    List* newList = (List*)calloc(1, sizeof(List));
    newList->front = NULL;
    newList->rear = NULL;
    newList->size = 0;
    return newList;
}

char* frontQueuePop(Queue* stack){
    char *ch = (char*)malloc(32);
    strcpy(ch, stack->front->data);
    node_t* pNode = stack->front;
    stack->front = stack->front->next;
    free(pNode);
    stack->size--;
    if(stack->size == 0) free(stack);
    return ch;
}

char* rearQueuePop(Queue* stack){
    char *ch = (char*)malloc(32);
    strcpy(ch, stack->rear->data);
    node_t* pNode = stack->front;
    while(pNode->next != stack->rear){
        pNode = pNode->next; 
    }  //指向rear前一位
    node_t* pRear = stack->rear;
    stack->rear = pNode;
    free(pRear);
    stack->size--;
    if(stack->size == 0) free(stack);
    return ch;
}

int rearInsert(List *list, char *data)
{
    node_t *newNode = (node_t *)calloc(1, sizeof(node_t));
    strcpy(newNode->data, data);
    newNode->next = NULL;
    if (list->size == 0)
    {
        list->front = newNode;
        list->rear = newNode;
    }
    else
    {
        list->rear->next = newNode;
        list->rear = newNode;
    }
    list->size++;
    return 0;
}

int frontInsert(List *list, char *data)
{
    node_t *newNode = (node_t *)calloc(1, sizeof(node_t));
    strcpy(newNode->data, data);
    newNode->next = NULL;
    if (list->size == 0)
    {
        list->front = newNode;
        list->rear = newNode;
    }
    else
    {
        newNode->next = list->front;
        list->front = newNode;
    }
    list->size++;
    return 0;
}
